package com.cn.core.result;

import com.cn.core.constant.HttpStatus;
import lombok.Data;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * @Author SHANGGUAN
 **/
@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public T getData() {
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    private T data;

    private int code;

    private String msg;

    public R() {

    }


    public R(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public R(String msg, int code) {
        this.msg = msg;
        this.code = code;
    }


    public static <T> R<T> ok() {
        return R.ok("success");
    }

    public static <T> R<T> ok(String msg) {
        return R.ok(null, msg, HttpStatus.HTTP_OK);
    }

    public static <T> R<T> ok(T data) {
        return R.ok(data, "success");
    }

    public static <T> R<T> ok(T data, Integer code) {
        return R.ok(data, "success", code);
    }

    public static <T> R<T> ok(T data, String msg) {
        return ok(data, msg, HttpStatus.HTTP_OK);
    }

    public static <T> R<T> ok(T data, String msg, Integer code) {
        return new R<T>(data, msg, code);
    }

    public static <T> R<T> fail() {
        return fail("fail", HttpStatus.HTTP_BAD_REQUEST);
    }

    public static <T> R<T> fail(String msg) {
        return fail(msg, HttpStatus.HTTP_BAD_REQUEST);
    }

    public static <T> R<T> fail(String msg, Integer code) {
        return new R<T>(msg, code);
    }


}
